package im.dhc.gitdiff;

import im.dhc.gitdiff.common.*;
import im.dhc.gitdiff.util.FilePathFilter;
import im.dhc.gitdiff.util.FileUtil;
import im.dhc.gitdiff.util.GitUtil;
import im.dhc.gitdiff.util.PathUtil;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.diff.DiffEntry;

import java.io.BufferedWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GitRep {
    
    public static void main(String[] args) throws Exception{
        String newVersion = "c6fd68e78845656998d68cf64c8a34fd7cf02abe";
        String oldVersion = "baa0848a8faba137eb7f0ea82df702b261a5b998";//		

        Git git = GitUtil.gitInit();
        
        List<DiffEntry> diffs = GitUtil.diffTwoBranch(git, GitUtil.getLocalBranchRef("develop"), GitUtil.getRemoteBranchRef("develop-weipeisong"));
        
        //GitUtil.diffTwoVersion(git, newVersion, oldVersion);
        //GitUtil.diffHeadAndVersion(git, oldVersion);

        BufferedWriter backupbw = FileUtil.getWriter("backup");
        BufferedWriter updatebw = FileUtil.getWriter("update");
        BufferedWriter recoverbw = FileUtil.getWriter("recover");

        Map<String,String> moduleMap = new HashMap<String,String>();
        for(int i=0;i<diffs.size();i++){
            DiffEntry diff = diffs.get(i);
            String changeType = diff.getChangeType().name();
            DiffHandler handler = null;
            System.out.println("-----changeType:"+changeType+"-----");
            System.out.println("-----oldPath:["+diff.getOldPath()+"]newPath:["+diff.getNewPath()+"]-----");

            if("ADD".equals(changeType)){
                if(FilePathFilter.filter(diff.getNewPath())){
                    if(PathUtil.isJavaFile(diff.getNewPath())){
                        String module = PathUtil.getModule(diff.getNewPath());
                        if(moduleMap.containsKey(module)){
                            continue;
                        }else{
                            moduleMap.put(module,"Y");
                        }
                    }
                    handler = new AddDiffHandler(null,diff.getNewPath(),backupbw,updatebw,recoverbw);
                }
            }else if("MODIFY".equals(changeType)){
                if(FilePathFilter.filter(diff.getNewPath())){
                    if(PathUtil.isJavaFile(diff.getNewPath())){
                        String module = PathUtil.getModule(diff.getNewPath());
                        if(moduleMap.containsKey(module)){
                            continue;
                        }else{
                            moduleMap.put(module,"Y");
                        }
                    }
                    handler = new ModifyDiffHandler(diff.getOldPath(),diff.getNewPath(),backupbw,updatebw,recoverbw);
                }
            }else if("DELETE".equals(changeType)){
                if(FilePathFilter.filter(diff.getOldPath())) {
                    if (PathUtil.isJavaFile(diff.getOldPath())) {
                        String module = PathUtil.getModule(diff.getOldPath());
                        if(moduleMap.containsKey(module)){
                            continue;
                        }else{
                            moduleMap.put(module,"Y");
                        }
                    }
                    handler = new DeleteDiffHandler(diff.getOldPath(), null, backupbw, updatebw, recoverbw);
                }
            }else if("RENAME".equals(changeType)){
                if(FilePathFilter.filter(diff.getNewPath())){
                    if(PathUtil.isJavaFile(diff.getNewPath())){
                        String module = PathUtil.getModule(diff.getNewPath());
                        if(moduleMap.containsKey(module)){
                            continue;
                        }else{
                            moduleMap.put(module,"Y");
                        }
                    }
                    handler = new RenameDiffHandler(diff.getOldPath(),diff.getNewPath(),backupbw,updatebw,recoverbw);
                }
            }else{
                System.out.println("没有处理文件-----"+diff.getNewPath());
            }

            if(handler != null){
                handler.backupHandle();
                handler.updateHandle();
                handler.recoverHandle();
                handler.copyFileHandle();
            }

        }

        backupbw.close();
        updatebw.close();
        recoverbw.close();

        git.close();
    }


}
